package com.ajie.bluetoothcard.ui;


import java.util.ArrayList;

import com.ajie.bluetoothcard.adapter.CardAdapter;
import com.ajie.bluetoothcard.db.DBApi;
import com.ajie.bluetoothcard.domain.Card;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class CardListActivity extends BaseActivity implements OnItemClickListener,OnItemLongClickListener{

	private CardAdapter cardAdapter;
	private ListView listView;
	private ArrayList<Card> cards = new ArrayList<Card>();
	private DBApi dbApi;
	private String cardType = "0";
	private Button search_button,del_button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		dbApi = DBApi.getInstance(this);
		showTop(R.string.card_list, false);
		cardType = getIntent().getStringExtra("cardType");
//		btn_next.setVisibility(View.VISIBLE);
//		btn_next.setText(R.string.add_card);
		initData();
		initView();
	}
	
	private void initData() {
		cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_ALL,"",cardType,"0");
	}
	
	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		super.initView();
		search_button = (Button) findViewById(R.id.search_button);
		search_button.setOnClickListener(this);
		del_button = (Button) findViewById(R.id.del_button);
		del_button.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.list);
		cardAdapter = new CardAdapter(this);
		listView.setAdapter(cardAdapter);
		cardAdapter.setData(cards);
		listView.setOnItemClickListener(this);
//		listView.setOnItemLongClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_next:
			startActivityForResult(new Intent(this,CardInfoEditActivity.class).putExtra("type","add"),10001);
			break;
		case R.id.del_button:
			startActivityForResult(new Intent(this,DelCardListActivity.class).putExtra("cards",cards).putExtra("isDel", false),10002);
			break;
		case R.id.search_button:
			LayoutInflater factory = LayoutInflater.from(this) ;
			View myView = factory.inflate(R.layout.add_card_type_edit, null) ;
			final EditText editText = (EditText) myView.findViewById(R.id.add_card_type_edit);
			Dialog dialog = new AlertDialog.Builder(this)
				.setTitle("请输入关键字")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(!"".equals(editText.getText().toString())){
							cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_CONDITION, editText.getText().toString(), cardType, "0");
							cardAdapter.setData(cards);
						}else{
							cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_ALL,"",cardType,"0");
							cardAdapter.setData(cards);
						}
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				}).setView(myView).create();
			dialog.setCanceledOnTouchOutside(false);
			dialog.show() ;
			break;

		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
		startActivityForResult(new Intent(this,MainMyCardActivity.class).putExtra("card", cards.get(position)),10001);
	}
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position,long id) {
		// TODO Auto-generated method stub
		showDialog(cards.get(position));
		return false;
	}
	protected void showDialog(final Card card) {
		new AlertDialog.Builder(this).setTitle("删除名片？")
		.setMessage("您确定要删除此名片吗？")
		.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				card.setIsDel("1");
				dbApi.updateRecord(card);
				cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_ALL,"",cardType,"0");
				cardAdapter.setData(cards);
			}
		}).setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				
			}
		}).create().show();
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 10001:
			cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_ALL,"",cardType,"0");
			cardAdapter.setData(cards);
			break;
		case 10002:
			cards = dbApi.getAllCardsByType(DBApi.SEARCHTYPE_ALL,"",cardType,"0");
			cardAdapter.setData(cards);
			break;

		default:
			break;
		}
	}
}
